package view.shop
{
import core.Language;

import flash.display.MovieClip;

import mx.utils.StringUtil;

import values.item.INumbericItemValue;
import values.item.ItemTemplateValue;
import values.shop.ShopItemValue;

import view.component.asset.IconProxy;

/**
 * ShopItemCosterProxy
 * @author jliang
 *
 */
public class ShopItemCosterProxy extends ShopItemHandlerProxy
{
    //==========================================================================
    //  Constructor
    //==========================================================================
    public function ShopItemCosterProxy()
    {
        super();
    }
    //==========================================================================
    //  Variables
    //==========================================================================
    protected function get  cost1():MovieClip
    {
        return skin.cost1;
    }
    
    protected function get  cost2():MovieClip
    {
        return skin.cost2;
    }
    
    protected function get  discount():MovieClip
    {
        return skin.discount;
    }
    
    protected function get cost1Icon():IconProxy
    {
        return proxys["cost1Icon"];
    }
    
    protected function get cost2Icon():IconProxy
    {
        return proxys["cost2Icon"];
    }
    //==========================================================================
    //  Properties
    //==========================================================================
    protected var _data:ShopItemValue;

    public function get data():ShopItemValue
    {
        return _data;
    }

    public function set data(value:ShopItemValue):void
    {
        _data = value;
        
        var costTemplate:ItemTemplateValue = INumbericItemValue(data.nowCost.costValueList[0]).template;
        
        var iconUrl:String = costTemplate.getIconPathByKey("ui");
        
        cost1.line.visible = cost2.line.visible = false;
        
        if (data.isInDiscount)
        {
            cost2.visible = true;
            cost2.line.visible = false;
            cost2Icon.load(iconUrl);        
            cost2.label.text = INumbericItemValue(data.nowCost.costValueList[0]).num;
            
            discount.visible = cost1.visible = cost1.line.visible = true;
            cost1Icon.load(iconUrl);            
            cost1.label.text = String(data.oldCostNum);
            cost1.line.width = cost1.label.textWidth + 4;
            
            var off:String = Number(10 * Number(cost2.label.text) / data.oldCostNum).toFixed(1);
            if (Number(off.split(".")[1]) == 0)
            {
                off = off.split(".")[0];
            }
            discount.label.text = StringUtil.substitute(Language.getText("@shop_discount_label"), off);
        }
        else
        {
            discount.visible = cost2.visible = false;
            cost1.visible = true;
            
            cost1Icon.load(iconUrl);        
            cost1.label.text = INumbericItemValue(data.nowCost.costValueList[0]).num;
        }
    }
    //==========================================================================
    //  Overridden methods: ComponentProxy
    //==========================================================================    
    override protected function get proxyMapping():Array
    {
        return [ 
            { skin:"cost1", classRef: IconProxy, id:"cost1Icon" },
            { skin:"cost2", classRef: IconProxy, id:"cost2Icon" }
        ];
    }
    
    override public function parse(target:MovieClip):void
    {
        super.parse(target);
    }
    
    override public function dispose():void
    {
        reset();
        
        super.dispose();        
    }
    //==========================================================================
    //  Public methods
    //==========================================================================
    public function reset():void
    {
        cost1Icon.stopAndRemove();
        cost2Icon.stopAndRemove();
    }
}
}




